<?php
/**
 * app端注册,账号密码登陆，找回密码
 * User: Administrator
 * Date: 2017/8/22
 * Time: 10:27
 */
defined('InNcPHP') or exit('Access Invalid!');

class app_registerControl extends  Control {

    public function __construct()
    {
        $this->_checkApiSign();//检查接口权限
    }

    /**
     * app用户注册
     */
    public function memberOp(){

        $phone=$_POST['mobile'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        $sms_code = $_POST['sms_code'];
        $nickname = $_POST['name'];
        $member_model = Model('member');
        if(empty($phone)){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'手机号码不能为空'));exit;
        }
        if(empty($password) || empty($confirm_password)){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'密码不能为空'));exit;
        }
        if($password !==$confirm_password){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'两次输入密码不一致'));exit;
        }
        if(empty($sms_code)){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'短信验证码不能为空'));exit;
        }
        $data = checkCode($phone,$sms_code);
        if(!isset($data['error'])){
            $res = $member_model->getMemberInfo(array('mobile'=>$phone));//获取用户是否存在
            if($res){
                echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'用户已经存在'));exit;
            }else{
                $result = array();
                $result['mobile'] = $phone;
                $result['password'] = md5(trim($password));
                $result['addtime'] = time();
                $result['is_merchant'] = 0;
                $result['belong_proxy'] = null;
                $result['nickname'] = $nickname;
                $res = $member_model->addmember($result);
                if($res){
                    $cache = Cache::getInstance(C('cache.type'));
                    $cache->rm($phone);//删除验证码缓存;
                    echo json_encode(array('code'=>2000,'data'=>array('success'=>1),'message'=>'注册成功'));exit;
                }else{
                    echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'用户已经存在'));exit;
                }
            }
        }else{
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>$data['error']));exit;
        }
    }


    /**
     * 获取验证码
     */
    public  function sendsmscodeOp(){
        require_once(BASE_DATA_PATH.'/vendors/sms/sms.php');
        $sms = new sms();
        $_code = rand(100000, 999999);
        $_mobile = $_POST['mobile'];
        $_content = "【互程】欢迎注册互程网，您的验证码为" . $_code;
        if(empty($_mobile)){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'手机号码不能为空'));exit;
        }else{

                if($sms->sendSMS($_content, $_mobile)){
                    $cache = Cache::getInstance(C('cache.type'));
                    $code_info['last_access'] = time();
                    $code_info['sms_mobile'] =  $_mobile;
                    $code_info['sms_mobile_code'] = $_code;
                    $cache->set($_mobile,$code_info);
                    echo json_encode(array('code'=>2000,'data'=>array('success'=>1),'message'=>'短信验证码发送成功'));exit;

                }else{
                    echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'短信验证码发送失败'));exit;
                }


        }


    }

    /**
     *用户登陆(手机号码登陆)
     */
    public function app_loginOp(){

        $phone = $_POST['mobile'];
        $password = $_POST['pass_word'];
        if(empty($phone) || empty($password)){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'登陆信息不完整'));exit;
        }
        $member_model = Model('member');
        $arr = array();
        $arr['mobile'] = $phone;
        $arr['password'] = md5(trim($password));
        $res = $member_model->getMemberInfo($arr);
        if($res){
            //登陆成功,生成新token
            $token = $this->_get_token($res['member_id'],$phone);
            echo json_encode(array('code'=>2000,'data'=>array('token'=>$token),'message'=>'登陆成功'));exit;
        }else{
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'用户名或密码错误'));exit;
        }


   }


    /**
     * 找回密码接口(通过短信验证码及手机号码找回)
     */
    public function find_passwordOp(){
        $phone = $_POST['mobile'];
        $sms_code = $_POST['sms_code'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        if(empty($phone) || empty($sms_code) || empty($password) || empty($confirm_password)){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'参数有误'));exit;
        }
        $data = checkCode($phone,$sms_code);
        if(!isset($data['error'])){
            $member_model = Model('member');
            $member_info = $member_model->getMemberInfo(array('mobile'=>$phone));//判断该用户是否存在
            if($member_info) {
                //说明存在,修改数据库密码
                $res = $member_model->updateMember(array('mobile'=>$phone),array('password'=>md5(trim($password))));
                if($res){
                    $cache = Cache::getInstance(C('cache.type'));
                    $cache->rm($phone);//删除验证码缓存;
                    echo json_encode(array('code'=>2000,'data'=>array('success'=>1),'message'=>'密码修改成功'));exit;
                }else{
                    echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'密码修改失败'));exit;
                }

            }else{
                echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'用户不存在'));exit;
            }
        }else{
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>$data['error']));exit;
        }



    }


    /**
     * 登陆生成token
     */
    private function _get_token($member_id, $member_name) {
        $model_mb_user_token = Model('mb_user_token');

        //重新登陆后以前的令牌失效
        //暂时停用
        $condition = array();
        $condition['member_id'] = $member_id;
        $condition['client_type'] = 'app';
        $model_mb_user_token->delMbUserToken($condition);

        //生成新的token
        $mb_user_token_info = array();
        $token = md5($member_name . strval(TIMESTAMP) . strval(rand(0,999999)));
        $mb_user_token_info['member_id'] = $member_id;
        $mb_user_token_info['member_name'] = $member_name;
        $mb_user_token_info['token'] = $token;
        $mb_user_token_info['login_time'] = TIMESTAMP;
        $mb_user_token_info['client_type'] = 'app';
        $result = $model_mb_user_token->addMbUserToken($mb_user_token_info);
        if($result) {
            return $token;
        } else {
            return null;
        }

    }

    // 接口权限判断
    private function _checkApiSign(){

        $sign = $_POST['sign'];

        //签名
        $sign_str      =$_POST['mobile'];

        if(empty($_POST['mobile'])){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'参数错误'));exit;
        }

        if($sign!=md5($sign_str.'lvbang')){

            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'签名错误'));exit;
        }

    }


}